package io.renren.modules.sys.service.impl;

import io.renren.common.utils.StringUtils;
import io.renren.modules.sys.dao.SysDictionariesDao;
import io.renren.modules.sys.entity.SysDictionariesEntity;
import io.renren.modules.sys.service.SysDictionariesService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import javax.validation.constraints.NotBlank;


@Service("sysDictionariesService")
public class SysDictionariesServiceImpl extends ServiceImpl<SysDictionariesDao, SysDictionariesEntity> implements SysDictionariesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        EntityWrapper<SysDictionariesEntity> entityWrapper = new EntityWrapper<>();
        String type = (String) params.get("type");
        entityWrapper.where("1=1");
        if (!StringUtils.isNull(type)) {
            entityWrapper.andNew("type={0}", type);
        }
        String code = (String) params.get("code");
        if (!StringUtils.isNull(code)) {
            entityWrapper.andNew("code={0}", code);
        }
        entityWrapper.orderBy("id", false);
        Page<SysDictionariesEntity> page = this.selectPage(
                new Query<SysDictionariesEntity>(params).getPage(),
                entityWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据type查询数据字典
     * @return
     */
    public List<SysDictionariesEntity> queryListByType(@NotBlank String type){
        EntityWrapper<SysDictionariesEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("1=1");
        entityWrapper.andNew("type={0}", type);
        entityWrapper.orderBy("sort", true);
        List<SysDictionariesEntity> list = this.selectList(
                entityWrapper
        );
        return list;
    }

}
